<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="java.util.Set"%>
<c:set var="title" scope="request">Sessions Monitoring</c:set>
<jsp:include page="/WEB-INF/jsp/inc/head.jsp" />

<%
if (request.getUserPrincipal() == null ){
	%>
	<p class="alert">
		<em>You must be logged in as an administrator to use this feature.</em>
	</p>
	<p>
		<a href="<%=request.getContextPath()%>/tools/profile">Go to Login page.</a>
	</p>
	<%
} else {
	try{
		//session.invalidate();
		//request.getSession(true);
		//weblogic.servlet.security.ServletAuthentication.weak("weblogic", "pcswladm01", request, response);
		/*Class vServletAuthentication = Class.forName("weblogic.servlet.security.ServletAuthentication");
		java.lang.reflect.Method vAuth = vServletAuthentication.getMethod("weak", new Class[]{String.class, String.class, HttpServletRequest.class, HttpServletResponse.class});
		vAuth.invoke(vServletAuthentication.getClass(), new Object[] {"weblogic", "weblogic", request, response});
		
		if (request.getUserPrincipal() != null ){
			out.print("request.getUserPrincipal().getName() = " + request.getUserPrincipal().getName()+ "<br>");
		} else {
			vAuth.invoke(vServletAuthentication.getClass(), new Object[] {"weblogic", "weblogic", request, response});
			if (request.getUserPrincipal() != null ){
				out.print("request.getUserPrincipal().getName() = " + request.getUserPrincipal().getName()+ "<br>");
			} else {
				out.print("request.getUserPrincipal().getName() = anonymous" + "<br>");
			}
		}
		*/
		String vSessionId = (String) request.getAttribute("session");
		if(vSessionId != null) {
			out.print("Following Session " + vSessionId + " <br />");
		}
		
		//vMBeanHome = weblogic.management.Admin.getInstance().getMBeanHome();
		Class vClass = Class.forName("weblogic.management.Admin");
		java.lang.reflect.Method vMethod= vClass.getMethod("getInstance", new Class[]{});
		Object vAdmin = vMethod.invoke(vClass.getClass(), null);
		vMethod = vAdmin.getClass().getMethod("getMBeanHome", new Class[]{});
		Object vMBeanHome = vMethod.invoke(vAdmin, null);
		if (vMBeanHome != null) {
			
			vMethod= vMBeanHome.getClass().getMethod("getAllMBeans", new Class[]{});
			Object vSet = vMethod.invoke(vMBeanHome, null);
			if (vSet != null && vSet instanceof java.util.Set) {
				java.util.Iterator vIt = ((java.util.Set)vSet).iterator();;
				while (vIt.hasNext()) {
					Object vObject = vIt.next();
					out.print("++obj = " + vObject.getClass().getName() + "<br>");
					try {
						//java.lang.reflect.Method vMethodgetServletSessionsMonitoringIds = vObject.getClass().getMethod("getServletSessionsMonitoringIds", null);
						//java.lang.reflect.Method vMethodgetOpenSessionsCurrentCount = vObject.getClass().getMethod("getOpenSessionsCurrentCount", null);
						//java.lang.reflect.Method vMethodgetOpenSessionsHighCount = vObject.getClass().getMethod("getOpenSessionsHighCount", null);
						java.lang.reflect.Method vMethodgetName = vObject.getClass().getMethod("getName", null);
						java.lang.reflect.Method vMethodgetServletSessions = vObject.getClass().getMethod("getServletSessions", null);
						Object vObjectName = vMethodgetName.invoke(vObject, null);
						out.print("--+ "+vObjectName+"<br>");
						/*
						try {
							Object vObjectResultgetName = vMethodgetName.invoke(vObject, null);
							java.lang.Object[]  vObjectServletSessions = (java.lang.Object[] )vMethodgetServletSessions.invoke(vObject, null);
							out.print("-+"+vObjectResultgetName+" NbSessions="+vObjectServletSessions.length+" <br>");
							
							java.lang.reflect.Method[] vMethods =  vObject.getClass().getMethods();
							for (int j=0; j<vMethods.length;j++){
								//out.print("---" + vObject.getClass().getName()+ " -> "+vMethods[j].getName()+" <br>");
								if (vMethods[j].getParameterTypes().length == 0 && ( vMethods[j].getName().startsWith("get") || vMethods[j].getName().startsWith("is") )) {
									try {
										Object vObjectResult = vMethods[j].invoke(vObject, null);
										if (vObjectResult != null) {
											out.print("---" + vMethods[j].getName() +" -> " + vObjectResult.getClass().getName()+" -> "+vObjectResult.toString()+" <br>");
										}
									} catch (Exception e){
									}
								}
							}						
						} catch (Exception e){
							out.print("-- Exception: "+ e + "<br>");
							StackTraceElement[] vStackTraceElement = e.getStackTrace();
							for (int i=0; i<vStackTraceElement.length;i++){
								out.print(vStackTraceElement[i]+ "<br>");
							}							
						}
						*/
						
						if (vMethodgetName!=null && vMethodgetServletSessions!=null) {
							try {
								Object vObjectResultgetName = vMethodgetName.invoke(vObject, null);
								java.lang.Object[]  vObjectServletSessions = (java.lang.Object[] )vMethodgetServletSessions.invoke(vObject, null);
								if (vObjectServletSessions != null && vObjectServletSessions.length != 0) {
									out.print("--+ "+vObjectResultgetName+"<br>");
									for (int i=0; i<vObjectServletSessions.length;i++){
										out.print("--+ ServletSessionRuntimeMBean -> " + vObjectServletSessions[i].getClass().getName() + "<br>");
										try {
											StringBuffer vStringBuffer = new StringBuffer();
											java.lang.reflect.Method[] vMethods =  vObjectServletSessions[i].getClass().getMethods();
											vStringBuffer.append("---" + vObjectServletSessions[i].getClass().getName());
											for (int j=0; j<vMethods.length;j++){
												out.print("---" + vObjectServletSessions[i].getClass().getName()+ " -> "+vMethods[j].getName()+" <br>");
												if (vMethods[j].getParameterTypes().length == 0 && ( vMethods[j].getName().startsWith("get") )) {
													try {
														Object vObjectResult = vMethods[j].invoke(vObjectServletSessions[i], null);
														if (vObjectResult != null) {
															vStringBuffer.append(" -"+vMethods[j].getName()+"="+vObjectResult.toString());
															out.print("-- "+ vMethods[j].getName()+"="+vObjectResult.toString()+"<br>");
														}
													} catch (Exception e){
													}
												}
											}
											vStringBuffer.append(vStringBuffer.toString() + "<br>");
											
										} catch (Exception e){
											out.print("--+ Exception: "+ e + "<br>");
											StackTraceElement[] vStackTraceElement = e.getStackTrace();
											for (int j=0; j<vStackTraceElement.length;j++){
												out.print(vStackTraceElement[j]+ "<br>");
											}											
										}
									}
								}
							} catch (Exception e){
								out.print("-- Exception: "+ e + "<br>");
								StackTraceElement[] vStackTraceElement = e.getStackTrace();
								for (int i=0; i<vStackTraceElement.length;i++){
									out.print(vStackTraceElement[i]+ "<br>");
								}							
							}						
						}
						
					
					} catch (NoSuchMethodException e){
						out.print("--+ No such method<br>");
					}
					/*
					java.lang.reflect.Method[] vMethods =  vObject.getClass().getMethods();
					for (int i=0; i<vMethods.length;i++){
						//if (vMethods[i].getParameterTypes().length == 0 && ( vMethods[i].getName().startsWith("get") || vMethods[i].getName().startsWith("is") )) {
						if (vMethods[i].getParameterTypes().length == 0 && ( vMethods[i].getName().startsWith("getName") || vMethods[i].getName().startsWith("getSession") )) {
							try {
								Object vObjectResult = vMethods[i].invoke(vObject, null);
								if (vObjectResult != null) {
									out.print("---" + vObject.getClass().getName()+"." +vMethods[i].getName()+" -> "+ vObjectResult.getClass().getName() + " -> " + vObjectResult.toString() + "<br>");
								} else {
									out.print("---" + vObject.getClass().getName()+"." +vMethods[i].getName()+" -> null <br>");
								}
							} catch (Exception e){
								out.print("--" + vObject.getClass().getName()+" invoke " +vMethods[i].getName()+" with Exception: "+ e + "<br>");
							}
						} else {
							//out.print("--" + vObject.getClass().getName()+" not invoke " +vMethods[i].getName() + "<br>");
						}
							//
						//}
					}
					*/
				}
			}
		}	
		
	} catch (Exception e) {
		out.print("++++++++++++++++++++++++++++++<br>");
		out.print(e+ "<br>");
		StackTraceElement[] vStackTraceElement = e.getStackTrace();
		for (int i=0; i<vStackTraceElement.length;i++){
			out.print(vStackTraceElement[i]+ "<br>");
		}
	}
}
%>

<jsp:include page="/WEB-INF/jsp/inc/foot.jsp" />